package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuary;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/v1/notice")
@Api(tags = "01.通知管理模块")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/add")
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "添加通知")
//    @ResponseBody
    public JsonResult addNotice(NoticeAddParam noticeAddParam) {
        Notice notice = new Notice();
        log.debug("noticeAddParam=" + noticeAddParam);
        BeanUtils.copyProperties(noticeAddParam, notice);
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "查询通知列表")
//    @ResponseBody
    public JsonResult noticeList(@ApiIgnore NoticeListQuary noticeListQuary) {
        log.debug("noticeListQuary=" + noticeListQuary);
        List<NoticeListVO> list = noticeMapper.selectNoticeList(noticeListQuary.getTitle(), noticeListQuary.getType());
//        List<NoticeListVO> list=noticeMapper.selectNoticeList(noticeListQuary);
        return JsonResult.ok(list);
    }

    @GetMapping("/detail")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "通知id", required = true, dataType = "Long")
    @ApiOperation(value = "查询通知详情")
//    @ResponseBody
    public JsonResult noticeDetailInfo(Long id) {
        log.debug("id=" + id);
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.selectNoticeDetailInfo(id);
        return JsonResult.ok(noticeDetailInfoVO);
    }

    @PostMapping("/del")
    @ApiOperationSupport(order = 40)
    @ApiOperation(value = "删除通知")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "通知id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "title", value = "通知标题")
    })
//    @ResponseBody
    public JsonResult delNotice(Long id,String title) {
        log.debug("id=" + id);
        noticeMapper.delNotice(id);
        return JsonResult.ok();
    }
    @PostMapping("/update")
    @ApiOperationSupport(order = 50)
    @ApiOperation(value = "修改通知")
//    @ResponseBody
    public JsonResult updateNotice(NoticeUpdateParam noticeUpdateParam) {
        Notice notice = new Notice();
        log.debug("noticeUpdateParam=" + noticeUpdateParam);
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        noticeMapper.updateNotice(notice);
        return JsonResult.ok();
    }
}
